package com.mall.utils;

import com.cloopen.rest.sdk.CCPRestSmsSDK;

import java.util.*;

/**
 * 短信发送工具类
 */
public class SmsUtil {

    private static final String serverIp = "app.cloopen.com";
    private static final String serverPort = "8883";
    private static final String accountId = "8a48b5515249574b015252af55751033";
    private static final String accountToken = "ed8667cafc7a46649781225d8a36b823";
    private static final String appId = "8a48b5515249574b015252c3cbf31091";


    public static List<Map<String,Object>>
    sendSmsToMobile(List<String>mobiles,String templateId,String []datas){

        List<Map<String,Object>> list = new ArrayList<>();

        Map<String,Object> result = null;//用于接收短信发送的结果
        CCPRestSmsSDK api = new CCPRestSmsSDK();

        api.init(serverIp,serverPort);
        api.setAccount(accountId,accountToken);
        api.setAppId(appId);
        for(String to:mobiles) {
            Map<String,Object> map = new HashMap<>();
            //执行发送
            result = api.sendTemplateSMS(to, templateId, datas);
            String code = result.get("statusCode").toString();
            String msg = "";
            if("000000".equals(code)){
                System.out.println("发送信息:"+result.get("data"));
                msg = String.format("信息码：%s,信息：%s。",code,result.get("statusMsg"));
                map.put("is",true);
            }else{
                msg = String.format("错误码：%s,错误信息：%s。",code,result.get("statusMsg"));
                System.out.println(msg);
                map.put("is",false);
            }
            map.put("time",new Date().getTime());
            map.put(to,msg);
            list.add(map);
        }
        return list;
    }

    public static void main(String[] args) {
        /*List<String>mobils = {"18577867151"};
        String datas[] = {"741258","5"};

        List<Map<String,Object>> list = sendSmsToMobile(mobiles,"1",datas);
        for(Map<String,Object> map:list){
            System.out.println(map.toString());
        }*/
    }

}
